<template>
	<view class="">
		<nav-bar title="地址管理"></nav-bar>
		<van-address-list @select="selectAddress"
		  v-model="chosenAddressId"
		  :list="list"
		  disabled-text="以下地址超出配送范围"
		  default-tag-text="默认"
		  @add="onAdd"
		  @edit="onEdit"
		/>

	</view>
</template>

<script>
	const Address = uniCloud.importObject("address")
	import Storage from '@/untils/storage.js'
	import {ref} from 'vue'
	export default {
	  setup() {
	    return {
	      chosenAddressId: ref(""),
	    };
	  },
	  data(){
		  return{
			  list:[]
		  }
	  },
	  onLoad(){
		this.getAddressList();
	  },
	  methods:{
		  onAdd(){
			  uni.navigateTo({
			  	url:"/pages/user/address/add"
			  })
		  },
		  getAddressList(){
			  Address.list(Storage.userinfo._id).then(res=>{
				  let list = res.data;
				  for (var i = 0; i < list.length; i++) {
				  	list[i].id = list[i]._id
					if(list[i].is_default){
						this.chosenAddressId = list[i]._id
					}
				  }
				  this.list = list
			  })
		  },
		  onEdit(e){
			uni.navigateTo({
			  	url:"/pages/user/address/edit?id="+e.id
			})
		   },
		  selectAddress(e){
			  this.chosenAddressId = e.id
			  Address.setDefault(e.user_id,e.id).then(res=>{
				  this.$store.commit("setDefaultAddress",e)
			  })
		  }
	  }
	};

</script>

<style>
</style>